//
//  NewHomeServiceView.m
//  NewPro
//
//  Created by RYX on 2019/4/24.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "NewHomeServiceView.h"
#import "NewHomeServiceCell.h"
#import "NewHomeServiceModel.h"
#import "TransactionViewController.h"
#import "WorkOrderViewController.h"
#import "PosRegistViewController.h"
#import "HomeList.h"

@interface NewHomeServiceView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UICollectionView *collectionView;

@property (nonatomic,copy) NSArray *listArr;
@property (nonatomic,copy) NSArray *vcArr;

@end

@implementation NewHomeServiceView

- (NSArray *)vcArr{
    
    if (!_vcArr) {
        
        _vcArr = @[[PosRegistViewController class],[TransactionViewController class],[WorkOrderViewController class]];
    }
    return _vcArr;
}

- (NSArray *)listArr{
    
    if (!_listArr) {
        
        NewHomeServiceModel *model1 = [[NewHomeServiceModel alloc] init];
        model1.title = @"POS开通";
        model1.detail = @"注册简单，快速审批";
        model1.img = @"POS开通图标";
        
        NewHomeServiceModel *model2 = [[NewHomeServiceModel alloc] init];
        model2.title = @"交易明细";
        model2.detail = @"历史记录，一键查询";
        model2.img = @"交易明细图标";
        
        NewHomeServiceModel *model3 = [[NewHomeServiceModel alloc] init];
        model3.title = @"工单管理";
        model3.detail = @"关爱商户，防范风险";
        model3.img = @"首页管理图标";
        
        _listArr = @[model1, model2, model3];
    }
    return _listArr;
}

- (UICollectionView *)collectionView{
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumLineSpacing = 0.5;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.itemSize = CGSizeMake((ScreenWidth - 1) / 3, 100);
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = BASECOLOR;
        
        
        [_collectionView registerNib:[UINib nibWithNibName:@"NewHomeServiceCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"CELL"];
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;

    }
    
    return _collectionView;
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = UIColor.whiteColor;
        [self setUI];
    }
    return self;
}

- (void)setUI{
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = BLUECOLOR;
    [self addSubview:line];
    
    UILabel *la2 = [[UILabel alloc] init];
    la2.font = [UIFont systemFontOfSize:14];
    la2.attributedText = [StrTool getAttributedTextWithTitle:@"商户服务" color:[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0] fontSize:14];
    [self addSubview:la2];
    
    UIView *line1 = [[UIView alloc] init];
    line1.backgroundColor = BASECOLOR;
    [self addSubview:line1];
    
    [self addSubview:self.collectionView];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.equalTo(self).offset(14);
        make.width.mas_equalTo(1.5);
        make.height.mas_equalTo(14);
    }];
    
    [la2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(line.mas_right).offset(2);
        make.centerY.equalTo(line);
    }];
    
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(self);
        make.top.equalTo(self).offset(43);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.collectionView  mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(line1.mas_bottom);
        make.left.right.bottom.equalTo(self);
    }];
}

- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
    
}


- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.listArr.count;
    
}

- (UICollectionViewCell *) collectionView:(UICollectionView *) collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    
    NewHomeServiceCell * cell = (NewHomeServiceCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
    cell.model = self.listArr[indexPath.row];
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (![[HomeList sharedInstance].hadRealName isEqualToString:@"4"]) {
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(judgeIfHadRealName)]) {
            
            [self.delegate judgeIfHadRealName];
            return;
        }
        return;
    }
    
    UIViewController *VC = [[self.vcArr[indexPath.row] alloc] init];
    [VC setHidesBottomBarWhenPushed:YES];
    [[self getCurrentViewController].navigationController pushViewController:VC animated:YES];
    
}

@end
